package com.rem.cloudalibaba.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 自定义日志过滤器
 *
 * @author Rem
 * @date 2021-09-22
 */

@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

    /**
     * 构造函数
     */
    public LogGatewayFilterFactory() {
        super(LogGatewayFilterFactory.Config.class);
    }

    /**
     * 读取配置文件的参数，赋值到配置类中
     *
     * @return
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("consoleLog", "cacheLog");
    }

    /**
     * 过滤器逻辑
     *
     * @param config
     * @return
     */
    @Override
    public GatewayFilter apply(LogGatewayFilterFactory.Config config) {
        return (exchange, chain) -> {
            if (config.isCacheLog()) {
                System.out.println("cacheLog 已经开启了");
            }
            if (config.isConsoleLog()) {
                System.out.println("console 已经开启了");
            }
            return chain.filter(exchange);
        };
    }

    @Data
    public static class Config {

        private boolean consoleLog;

        private boolean cacheLog;

    }

}
